/*
    Copyright 2008, 2009, 2010 Brightworks, Inc.

    This file is part of Language Mentor.

    Language Mentor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Language Mentor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.controller
{

    import com.brightworks.util.Debug;
    import com.brightworks.util.Utils_Math;
    import com.langcollab.languagementor.constant.Constant_Misc;
    import com.langcollab.languagementor.event.Event_Download;
    import com.langcollab.languagementor.model.Callbacks;
    import com.langcollab.languagementor.model.DataManager;
    import com.langcollab.languagementor.model.MainModel;
    import com.langcollab.languagementor.util.Utils_System;

    import flash.events.Event;
    import flash.filesystem.File;
    import flash.system.Capabilities;

    import mx.controls.ToolTip;
    import mx.rpc.events.FaultEvent;

    import org.swizframework.utils.commands.IEventAwareCommand;

    public class Command_InitApplication extends Command_Base implements IEventAwareCommand
    {
        [Inject]
        public var model:MainModel;

        // ****************************************************
        //
        //
        //          Public Methods
        //
        //
        // ****************************************************

        public function Command_InitApplication()
        {
            super();
        }

        public function execute():void
        {
            ToolTip.maxWidth = 150;
            model.audioFileDirectoryURL = File.applicationStorageDirectory.nativePath + File.separator + Constant_Misc.FILEPATHINFO_MODULE_FOLDER_NAME;
            ensureDBFileExists();
            var callbacks:Callbacks = new Callbacks(result_InitDataManager, fault);
            model.dataManager.init(callbacks);
        }

        override public function fault(info:Object = null):void
        {
            if ((info is FaultEvent) || (info is String)) {
                Debug.error_Fatal(["Command_InitApplication.fault():", info]);
            }
            else {
                Debug.error_Fatal(["Command_InitApplication.fault(): Error info type not handled", info]);
            }
        }

        // ****************************************************
        //
        //          Private Methods
        //
        // ****************************************************

        private function ensureDBFileExists():void 
        {
            var dbFile:File = File.applicationStorageDirectory.resolvePath(
                Constant_Misc.FILEPATHINFO_DB_FOLDER_NAME +
                File.separator +
                Constant_Misc.FILEPATHINFO_DB_FILE_FILE_NAME   );
            if (!dbFile.exists) {
                var dbTemplateFile:File = File.applicationDirectory.resolvePath(
                    Constant_Misc.FILEPATHINFO_DB_TEMPLATE_FOLDER_NAME   +
                    File.separator +
                    Constant_Misc.FILEPATHINFO_DB_TEMPLATE_FILE_NAME   );
                dbTemplateFile.copyTo(dbFile, true);
            }
        }

        private function result_InitDataManager(info:Object):void
        {
            Debug.info("Command_InitApplication.result_InitDataManager() - download LM desc'ns");
            var e:Event_Download = new Event_Download(Event_Download.DOWNLOAD_LEARNING_MODE_DESCRIPTIONS);
            var callbacks:Callbacks = new Callbacks(result_DownloadLearningModeDescriptions, fault);
            e.callbacks = callbacks;
            dispatchEvent(e);
        }

        private function result_DownloadLearningModeDescriptions(info:Object):void
        {
            Debug.info("Command_InitApplication.result_DownloadLearningModeDescriptions() - download repos data");
            //// beginning of kludge download process
            if (Utils_System.isRunningOnDesktop())
            {
                model.currentRepositoryRootURL = "http://localhost:8080/modules/langcollab/eng/cmn";
            }
            else
            {
                model.currentRepositoryRootURL = "http://modules.languagecollaborative.com/langcollab/eng/cmn";
            }
            var e:Event_Download = new Event_Download(Event_Download.DOWNLOAD_REPOSITORY_DATA);
            var callbacks:Callbacks = new Callbacks(result_DownloadRepositoryData, fault);
            e.callbacks = callbacks;
            dispatchEvent(e);
        }

        private function result_DownloadModules(info:Object):void
        {
            Debug.info("Command_InitApplication.result_DownloadModules() - command is finished");
            // The final step has succeeded - we do nothing
        }

        private function result_DownloadRepositoryData(info:Object):void
        {
            Debug.info("Command_InitApplication.result_DownloadRepositoryData() - disp download modules event");
            var moduleCount:int = model.dataManager.getModuleVersionCount_ForCurrentNativeAndTargetLanguages();
            if (moduleCount == 0) 
            {
                var e:Event_Download = new Event_Download(Event_Download.DOWNLOAD_MODULES);
                e.publishedModuleVersionIdList = [
                    "com.languagecollaborative.0000000001"
                    ];
                /*e.publishedModuleVersionIdList = [
                    "com.languagecollaborative.0000000001",
                    "com.languagecollaborative.0000000002",
                    "com.languagecollaborative.0000000003",
                    "com.languagecollaborative.0000000004",
                    "com.languagecollaborative.0000000005",
                    "com.languagecollaborative.0000000006",
                    "com.languagecollaborative.0000000007",
                    "com.languagecollaborative.0000000008",
                    "com.languagecollaborative.0000000009"
                ];*/
                e.contentProviderId = "com.languagecollaborative.eng.cmn";
                var callbacks:Callbacks = new Callbacks(result_DownloadModules, fault);
                e.callbacks = callbacks;
                dispatchEvent(e);
            }

        }

    }
}








